<?php
namespace Period\Controller;

use Book\Model\BookModel;
use Common\Controller\ApiController;
use Common\Model\ModelException;

class UserPeriodController extends ApiController
{
    protected  $Model = NULL; 
    public function _initialize() {
        parent::_initialize();
        $this->Model = D(CONTROLLER_NAME);
    }
    /*
     * 添加宝宝当前状态
     * period:'baby','pregnant','parenting' => 1备孕中 2怀孕中 3宝宝出生
     */
    public function addPeriod() {
        $input = $this->Request();
        $input['user_id'] = $this->user['id'];
        $input['add_time'] = time();
        if($this->Model->create($input)){
            try {
                $this->Model->add();
                $this->asyncResponse();
            } catch (Exception $exc) {
                $this->errorResponse(70001, $exc->getMessage());
            }
        }
    }
    
    /*
     *更新和删除
     */
    public function updatePeriod() {
        $input = $this->Request();
        $uid = $this->user['id'];
        if(isset($input['delete_time'])){//删除状态操作
            $input['delete_time'] = strtotime($input['delete_time']);
        }
        if(isset($input['select'])){//更新选中状态前先把之前的选中变为0
            $this->Model->where(['user_id'=>$uid])->setField('select',0);
        }
        if($this->Model->create($input)){
            try {
                $this->Model->save();
                $this->asyncResponse();
            } catch (Exception $exc) {
                $this->errorResponse(70001, $exc->getMessage());
            }
        }
    }
   
    
    /*
     * 查询
     */
    public function period() {
        $uid = $this->user['id'];
        $period = $this->Model->where(['user_id'=>$uid,'delete_time'=>0])->select();
        $this->response($period);
    } 
}
